var ReturnVisit = {//回访详情  初始化
    id: "ReturnVisitTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

ReturnVisit.initColumn = function () {//初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.customerAge == "" || row.customerAge == null || row.customerAge == "null") {
                    return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                } else if (row.customerAge >= 0 && row.customerAge < 18) {
                    if (row.customerSex == 1) {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerSex == 2) {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }
                } else if (row.customerAge >= 18 && row.customerAge <= 50) {
                    if (row.customerSex == 1) {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerSex == 2) {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }
                } else if (row.customerAge > 50) {
                    if (row.customerSex == 1) {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerSex == 2) {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else {
                        return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }
                }
            }
        },
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true, width: 110},
        {
            title: '首次到店', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 90},
        {
            title: '回访内容', field: 'huifang', align: 'center', valign: 'middle', sortable: true, width: 250,
            formatter: function (value, row, index) {
                return '<span title="' + value + '">' + value + '</span>'
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
            }
        },
        {
            title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        }
    ];
    return columns;
};

ReturnVisit.search = function () {//查询 回访详情列表
    ReturnVisit.table.refresh({query: ReturnVisit.formParams()});
};

ReturnVisit.resetSearch = function () {
    $("#outpatientName").val(""); //  门诊
    $("#doctorTcInputPublic", parent.document).val("");
    $("#startTime").val(""); //开始时间
    $("#endTime").val("");  //结束时间
    $("#tcName").val("");   //tc
    $(".timeBox > div button").removeClass('actionClick');
    ReturnVisit.search();
};

ReturnVisit.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val();  //门诊
    queryData['sreach'] = parent.$("#doctorTcInputPublic").val();  //姓名   电话   微信  预约号
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['tcName'] = $("#tcName").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    return queryData;
};

ReturnVisit.today = function () {// 今日
    var date = new Date();
    var currentYear = date.getFullYear();       // 当前年份
    var currentMonth = date.getMonth() + 1;    // 当前月份
    var currentDay = date.getDate();         // 今日日期 几号
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    document.getElementById("startTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00";
    document.getElementById("endTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59";
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    ReturnVisit.search();
};

ReturnVisit.tomorrow = function () {// 明日
    var date = new Date();
    date.setDate(date.getDate() + 1);
    var currentYear = date.getFullYear();       // 当前年份
    var currentMonth = date.getMonth() + 1;    // 当前月份
    var currentDay = date.getDate();         // 今日日期 几号
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    document.getElementById("startTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00";
    document.getElementById("endTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59";
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    ReturnVisit.search();
};

ReturnVisit.thisWeek = function () {// 本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    var weekTimeStartYear = monday.getFullYear();       // 当前年份
    var weekTimeStartMonth = monday.getMonth() + 1;    // 当前月份
    var weekTimeStartDay = monday.getDate();         // 今日日期 几号
    if (weekTimeStartMonth < 10) weekTimeStartMonth = "0" + weekTimeStartMonth;
    if (weekTimeStartDay < 10) weekTimeStartDay = "0" + weekTimeStartDay;
    var weekTimeEndYear = sunday.getFullYear();       // 当前年份
    var weekTimeEndMonth = sunday.getMonth() + 1;    // 当前月份
    var weekTimeEndDay = sunday.getDate();         // 今日日期 几号
    if (weekTimeEndMonth < 10) weekTimeEndMonth = "0" + weekTimeEndMonth;
    if (weekTimeEndDay < 10) weekTimeEndDay = "0" + weekTimeEndDay;
    document.getElementById("startTime").value = weekTimeStartYear + "-" + weekTimeStartMonth + "-" + weekTimeStartDay + " " + "00:00:00";
    document.getElementById("endTime").value = weekTimeEndYear + "-" + weekTimeEndMonth + "-" + weekTimeEndDay + " " + "23:59:59";
    $(".timeBox > div button").eq(2).addClass('actionClick').siblings().removeClass('actionClick');
    ReturnVisit.search();
};

$("#doctorTcSearchAll", parent.document).click(function () {//主页搜索
    ReturnVisit.search();
});

$(function () {
    var defaultColunms = ReturnVisit.initColumn();
    var table = new BSTable(ReturnVisit.id, "/returnVisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ReturnVisit.formParams());
    ReturnVisit.table = table.init();
    ReturnVisit.search();
    ReturnVisit.today();
    menuNameHideColumn($("#menuName").val(),'ReturnVisitTable');
    var seletetcName = $('#tcNameValue').val();
    if (seletetcName != "") {
        $("#tcName option[value='" + seletetcName + "']").attr("selected", "selected");
    }
    var seleteooutpatientName = $('#outpatientNameValue').val();
    if (seleteooutpatientName != "") {
        $("#outpatientName option[value='" + seleteooutpatientName + "']").attr("selected", "selected");
    }
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {
        ReturnVisit.search();
    });
    checkDate = function () {
        ReturnVisit.search();
    };
    $("#zoom").click(function () {
        if ($("#zoom > span").hasClass('tabMark')) {
            $("#zoom > span").removeClass('tabMark').html('条件展开');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready(function () {
                $('#ReturnVisitTable').bootstrapTable('resetView', {height: $(window).height() - 76});
            });
        } else {
            $("#zoom > span").addClass('tabMark').html('条件收起');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready(function () {
                $('#ReturnVisitTable').bootstrapTable('resetView', {height: $(window).height() - 137});
            });
        }
    });
});